package com.striker.nettyconn;

import io.netty.channel.ChannelHandlerContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
*继承我们自己编写的枢纽站
*/
public class Client3Handler extends Middleware {
    private final Log logger = LogFactory.getLog(Client.class);

    private Client client;

    public Client3Handler(Client client) {
        super("client");
        this.client = client;
    }

    @Override
    protected void processData(ChannelHandlerContext ctx, Object msg) {
        // TODO Auto-generated method stub
        Model model = (Model) msg;
        logger.info("client  收到数据： " + model.toString());
//        if(model.getType() == TypeData.LOGIN) {
//            try {
//                client.sendData();
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//        }
    }
    
    @Override
    protected void handlerAllIdle(ChannelHandlerContext ctx) {
        // TODO Auto-generated method stub
        super.handlerAllIdle(ctx);
        sendPingMsg(ctx);
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        // TODO Auto-generated method stub
        super.channelInactive(ctx);
        client.doConnect();
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
            throws Exception {      
        logger.info(name + "exception :"+ cause.toString());
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        // TODO Auto-generated method stub
        logger.info(" ---"+ctx.channel().remoteAddress() +"----- channel is active|连接建立" );
    }

    // 对于客户端来说，输入来自控制台
    protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
        //仅仅打印来自服务端的信息
        logger.info(msg);
    }
}

